﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using QFramework;

namespace HKTools
{
    /// <summary>
    /// RoamingCam 鼠标点击触发位移 控制脚本
    /// </summary>
    [DisallowMultipleComponent]
    public class HK_RoamPointSelect : HKRoamingCam_BaseController
    {
        IHK_RoamingCamSystem roamingCamSystem;

        GameObject cursor_Go;

        // 摄像机的射线检测
        RaycastHit hit;
        bool isMouseMoving;

        void Awake()
        {
            roamingCamSystem = this.GetSystem<IHK_RoamingCamSystem>();
            cursor_Go = transform.Find("Cursor").gameObject;
        }

        void Update()
        {
            if (!roamingCamSystem.IsCanRotCam.Value)
            {
                if (cursor_Go.activeInHierarchy)
                    cursor_Go.SetActive(false);

                return;
            }

            if (roamingCamSystem.IsMobile)
                TouchFollow();
            else
                MouseFollow();
        }

        Vector2 initTouchPos;
        Vector2 finalTouchPos;

        //移动端  
        void TouchFollow()
        {
            if (Input.touchCount == 1)
            {
                if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                    return;

                if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
                {
                    if (hit.collider == null || hit.collider.tag != "RoamPoint")
                        return;

                    if (Input.GetTouch(0).phase == TouchPhase.Began)
                    {
                        initTouchPos = Input.GetTouch(0).position;
                    }

                    if (Input.GetTouch(0).phase == TouchPhase.Ended)
                    {
                        finalTouchPos = Input.GetTouch(0).position;

                        if (Vector2.Distance(initTouchPos, finalTouchPos) < 10)
                            CalcDis(hit.point);
                    }
                }
            }
        }

        //鼠标
        void MouseFollow()
        {
            // 如果触碰到UI 
            if (EventSystem.current.IsPointerOverGameObject())
            {
                if (cursor_Go.activeInHierarchy)
                    cursor_Go.SetActive(false);

                return;
            }

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                if (hit.collider == null || hit.collider.tag != "RoamPoint")
                {
                    if (cursor_Go.activeInHierarchy)
                        cursor_Go.SetActive(false);

                    return;
                }
                else if (roamingCamSystem.CurViewMode.Value == ViewMode.RoamView)
                {
                    if (!cursor_Go.activeInHierarchy)
                        cursor_Go.SetActive(true);
                }

                cursor_Go.transform.position = new Vector3(hit.point.x, hit.point.y + 0.01f, hit.point.z);

                if (Mathf.Abs(Input.GetAxis("Mouse Y")) >= 0.1f || Mathf.Abs(Input.GetAxis("Mouse X")) >= 0.1f)
                    isMouseMoving = true;

                // 鼠标移动后，提起鼠标0.05s之后，切换为静止状态
                if (isMouseMoving && !Input.GetMouseButton(0))
                    StartCoroutine(CancelMouseMoving_IE());

                if (Input.GetMouseButtonUp(0) && !isMouseMoving)
                    CalcDis(hit.point);
            }
            else
            {
                if (cursor_Go.activeInHierarchy)
                    cursor_Go.SetActive(false);
            }
        }

        IEnumerator CancelMouseMoving_IE()
        {
            yield return new WaitForSeconds(0.05f);
            isMouseMoving = false;
        }

        void CalcDis(Vector3 _hitPos)
        {
            List<float> disList = new List<float>();

            for (int i = 0; i < roamingCamSystem.points.Count; i++)
                disList.Add(Vector3.Distance(roamingCamSystem.points[i].TargetPos, _hitPos));

            float minDis = disList[0];
            int index = 0;

            // 求出最近距离TeleportPoint的索引
            for (int i = 0; i < disList.Count; i++)
            {
                if (disList[i] > minDis)
                    continue;

                minDis = disList[i];
                index = i;
            }

            // 如果角色离最近的目标点小于一米，则不位移过去
            if (Vector3.Distance(roamingCamSystem.points[index].TargetPos, transform.position) < 1f)
            {
                disList.Clear();
                return;
            }

            if (roamingCamSystem.CurViewMode.Value == ViewMode.BirdView || roamingCamSystem.CurViewMode.Value == ViewMode.ThreeDView)
                this.SendCommand(new CMD_ChangeViewMode(ViewMode.RoamView, roamingCamSystem.points[index]));
            else
                this.SendCommand(new CMD_SwitchRoamPoint(roamingCamSystem.points[index]));

            disList.Clear();
        }
    }
}